package com.duobaoyu.dby.supply.biz.service.refund.event;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.duobaoyu.dby.channel.adapter.enums.BizScenarioEnum;
import com.duobaoyu.dby.channel.adapter.enums.biz.refund.AfterSaleStatusEnum;
import com.duobaoyu.dby.common.core.constant.CommonConstants;
import com.duobaoyu.dby.common.mybatis.util.EnumUtils;
import com.duobaoyu.dby.message.dto.MessageParam;
import com.duobaoyu.dby.supply.biz.producer.SupplyChainMQProducer;
import com.duobaoyu.dby.supply.biz.service.queue.core.DistributionQueueMessage;
import com.duobaoyu.dby.supply.biz.service.queue.service.DistributionQueueJobService;
import com.duobaoyu.dby.supply.common.biz.bo.refund.AfterSaleAuditRuleBO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.List;
import java.util.Objects;

import static com.duobaoyu.dby.supply.biz.producer.constant.SupplyChainMQConstants.SupplyChainTopic.AFTER_SALE_TOPIC;
import static com.duobaoyu.dby.supply.biz.producer.constant.SupplyChainMQConstants.SupplyChainTopic.AfterSaleTag.AFTER_SALES_STATUS_CHANGE;

/**
 * Created by shuming on 2023/10/23 11:24
 * 售后创建事件监听
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AfterSaleCreateEventListener {

    private final SupplyChainMQProducer supplyChainMQProducer;

    private final DistributionQueueJobService distributionQueueJobService;

    @Value("${delay.submitChannelAfterSale.topicName}")
    private String delaySubmitChannelAfterSaleTopicName;

    @TransactionalEventListener
    public void handleAfterSaleOrderEvent(AfterSaleCreateEvent event) {
        try {
			if (EnumUtils.eq(AfterSaleStatusEnum.TO_BE_SUBMITTED_FOR_CHANNEL, event.getAfterSaleStatus())) {
                if (event.getOrderAutoCancel()) {
                    AfterSaleStateChangeEvent changeEvent = new AfterSaleStateChangeEvent();
                    changeEvent.setAfterSaleSn(event.getAfterSaleSn());
                    changeEvent.setAfterSaleType(event.getAfterSaleType());
                    changeEvent.setOrderSn(event.getOrderSn());
                    changeEvent.setAfterSaleStatus(event.getAfterSaleStatus());
                    changeEvent.setOrderAutoCancel(event.getOrderAutoCancel());
                    changeEvent.setRefundSuccess(false);
                    supplyChainMQProducer.afterSaleApplicationMsgSend(AFTER_SALE_TOPIC, AFTER_SALES_STATUS_CHANGE, changeEvent);
                }
            }
			AfterSaleAuditRuleBO auditRule = event.getAuditRule();
			boolean delaySubmit = Objects.nonNull(auditRule.getLateSubmitTime())
					&& (auditRule.getLateSubmitTime().compareTo(Long.valueOf(CommonConstants.ZERO))) > 0;
			if (EnumUtils.eq(AfterSaleStatusEnum.WAIT_DBY_AUDIT, event.getAfterSaleStatus()) && delaySubmit) {
				DistributionQueueMessage delayMessage =
						new DistributionQueueMessage(delaySubmitChannelAfterSaleTopicName,
								BizScenarioEnum.DELAY_SUBMIT_CHANNEL_AFTER_SALE.getValue(),
								event.getAfterSaleSn(),
								auditRule.getLateSubmitTime(), JSON.toJSONString(event));
				distributionQueueJobService.send(delayMessage);
			}
            if (!event.getOrderAutoCancel()) {
                MessageParam messageParam = new MessageParam();
                messageParam.setOrderNo(event.getOrderSn());
                List<AfterSaleCreateEvent.Sku> skuList = event.getSkuList();
                messageParam.setProductId(CollUtil.getFirst(skuList).getSkuCode());
                messageParam.setProductName(CollUtil.getFirst(skuList).getSkuName());
                supplyChainMQProducer.sendAfterSaleMailMessage(messageParam);
            }
        } catch (Exception e) {
            log.warn("售后创建事件监听异常,param={}", JSON.toJSONString(event), e);
        }
    }
}
